package com.web.manager.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.web.manager.pojo.Comment;
import com.web.manager.pojo.Result;
import com.web.manager.service.CommentService;
import com.web.manager.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@CrossOrigin  //解决跨域问题
@RestController
@RequestMapping("/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;


    //新增用户
    @PostMapping("/add")
    public Result add(@RequestBody Comment comment){

        if(comment.getId() == null){
            comment.setUserId(TokenUtils.getCurrentUser().getId());
            comment.setTime(DateUtil.now());

            if(comment.getPid() !=null){
                Integer pid =  comment.getPid();
                Comment pComment=commentService.getById(pid);
                if(pComment.getOriginId() != null){   //如果当前回复的父级有祖宗，就设置相同祖宗
                    comment.setOriginId(pComment.getOriginId());
                } else {   //否则设置父级为当前评论祖宗
                    comment.setOriginId(comment.getPid());
                }
            }

        }

        commentService.save(comment);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result update(@RequestBody Comment comment){
        commentService.updateById(comment);  //updateUser
        return Result.success();
    }


    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){

        commentService.removeById(id);  //deleteUser
        return Result.success();
    }

    @DeleteMapping("/delete/batch")
    public Result batchDelete(@RequestBody List<Integer> ids){

        commentService.removeBatchByIds(ids);   //batchDeleteUser
        return Result.success();
    }

    /*
    @GetMapping("/selectAll")
    public Result selectAll(){
        List<User> userList = userService.list();  //selectAll
        return Result.success(userList);
    }

     */
    //查询公告
    /*@GetMapping("/selectUserData")
    public Result selectUserData(){
        QueryWrapper<Comment> queryWrapper = new QueryWrapper<Comment>().orderByDesc("id");
        queryWrapper.eq("open",1);
        List<Notice> noticeList = noticeService.list(queryWrapper);  //selectAll
        return Result.success(noticeList);
    }*/



    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){

        Comment comment= commentService.getById(id);   //selectById
        return Result.success(comment);
    }



    @GetMapping("/tree/{articleId}")
    public Result findCommentDetail(@PathVariable Integer articleId){
        List<Comment> articleComments = commentService.findCommentDetail(articleId);

        List<Comment> originList = articleComments.stream().filter(comment -> comment.getOriginId() == null).collect(Collectors.toList());
        for(Comment origin : originList){
            List<Comment> comments = articleComments.stream().filter(comment -> origin.getId().equals(comment.getOriginId())).collect(Collectors.toList());
            comments.forEach(comment -> {
                articleComments.stream().filter(c1->c1.getId().equals(comment.getPid())).findFirst().ifPresent((v ->{
                    comment.setPUserId(v.getUserId());
                    comment.setPUsername(v.getUsername());
                }));
            });
            origin.setChildren(comments);
        }

        return Result.success(originList);

    }


    /**
     * 多条件模糊查询用户信息
     * pageNum 当前的页码
     * pageSize 每页查询的个数
     */
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize) {

        QueryWrapper<Comment> queryWrapper = new QueryWrapper<>();  //条件构造器
        Page<Comment> page = commentService.page(new Page<>(pageNum,pageSize), queryWrapper);        //selectByPage
        return Result.success(page);
    }



}
